package com.example.time_planner.fragments;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.time_planner.R;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.Legend;
import com.github.mikephil.charting.utils.Legend.LegendPosition;

public class PieChartFragment extends Fragment {

	private static final String[] CATS = { "Sleep", "Sport", "Work", "Family",
			"Relax" };

	private static final int MILLIS_FOR_ANIMATION_BUILDING_PIECHART = 1500;
	// List of colors for the chart
	private ArrayList<Integer> colors = new ArrayList<Integer>();

	// List of entries for the chart
	private ArrayList<Entry> yVals = new ArrayList<Entry>();

	// List of names of the Categories
	private ArrayList<String> xVals = new ArrayList<String>();

	private PieDataSet dataSet;// data for the chart Set type

	private PieChart pieChartMain;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.pie_chart_fragment, null);
		pieChartMain = (PieChart) view.findViewById(R.id.pie_chart);
		buildingPieChart();
		return view;
	}

	private void buildingPieChart() {
		addingDataToChart();
		pieChartMain.setUsePercentValues(true);
		pieChartMain.animateY(MILLIS_FOR_ANIMATION_BUILDING_PIECHART);
		Legend legend = pieChartMain.getLegend();
		legend.setPosition(LegendPosition.NONE);
		pieChartMain.setDrawHoleEnabled(false);
		pieChartMain.setDescription("");
	}

	private void addingDataToChart() {

		for (int index = 0; index < 5; index++) {
			yVals.add(new Entry(70f, index));
		}

		dataSet = new PieDataSet(yVals, "");

		for (int index = 0; index < 5; index++) {
			xVals.add(CATS[index]);
		}
		setUpTheColorsOfTheChart();
		pieChartMain.setData(new PieData(xVals, dataSet));
	}

	private void setUpTheColorsOfTheChart() {

		for (int color : ColorTemplate.COLORFUL_COLORS) {
			colors.add(color);
		}
		dataSet.setColors(colors);
	}
}
